//
//  TablesAppDelegate.m
//  Tables
//
//  Created by mathias Muller on 8/20/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//
#import "TablesAppDelegate.h"
#import "NumberDataSource.h"
#import "Challenge.h"
#import "ResultController.h"
#import "EasyChallenge.h"
#import "MediumChallenge.h"
#import "HardChallenge.h"

@implementation TablesAppDelegate

@synthesize window;
@synthesize navigationController,gameController, resultController, rootViewController;
@synthesize gameView;
@synthesize newChallenge;

- (void)dealloc {
	[window release];
	[navigationController release];
	[gameController release];
	[resultController release];
	[rootViewController release];
	[gameView release];		
	[newChallenge release];	
	[super dealloc];
}


- (id)init {
	if (self = [super init]) {
	}
	return self;
}


- (void)applicationDidFinishLaunching:(UIApplication *)application {
	// Configure and show the window
	[window addSubview:[navigationController view]];
	//self.navigationController.delegate = self;
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayResult:) name:@"Game_Ended" object:NULL];

	[window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
	// Save data if appropriate
}

-(void)startGame:(ChallengeConfiguration *) conf {
	[navigationController pushViewController:gameController animated:YES];
	Challenge * c = [Challenge startNew:conf];
	[gameController init:c];
	[c startTimer];
}

-(IBAction)startEasyGame:(id)sender {
	[self startGame:[[[EasyChallenge alloc] init] autorelease]];
}
-(IBAction)startMedGame:(id)sender {
	[self startGame:[[[MediumChallenge alloc] init] autorelease]];
}
-(IBAction)startHardGame:(id)sender {
	[self startGame:[[[HardChallenge alloc] init] autorelease]];
}
	
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
	if([viewController isEqual: gameController]){
		
	} else 	if([viewController isEqual: resultController]){
		
	} else 	if([viewController isEqual: rootViewController]){
		//will go back to home
		[gameController cancelGame];
	}

}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
	if([viewController isEqual: gameController]){

	} else 	if([viewController isEqual: resultController]){
		
	} else 	if([viewController isEqual: rootViewController]){
		//back to home
	}
}
		
-(IBAction)displayHome:(id)sender {
	[navigationController popToRootViewControllerAnimated:TRUE];
}


- (void) displayResult:(NSNotification*)notification {
	Challenge * c = (Challenge *)[notification object];
	[c stopTimer];
	[resultController displayResult:c];
	[navigationController pushViewController:resultController animated:YES];
}

@end
